﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;
using System.Collections.ObjectModel;
using System.Collections;

namespace BitPortal.Admin.Product.Product
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "ProductView", useExceptionHandler: true);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                datetimeDisplay.SelectedDate = DateTime.Now;
            }
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            wAddEdit.AutoLoad.Url = "ProductAddEdit.aspx?ReloadID=" + GridPanel1.ClientID;
            wAddEdit.LoadContent();
            wAddEdit.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                wAddEdit.AutoLoad.Url = "ProductAddEdit.aspx?ProductID=" + sm.SelectedRows[0].RecordID + "&ReloadID=" + GridPanel1.ClientID;
                wAddEdit.LoadContent();
                wAddEdit.Show();
            }

        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            List<Models.Product> data;
            if (Session["ProductGridFlag"] != null && Session["ProductGridFlag"].Equals(false))
            {
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                data = bitEnt.Products.Where(ent => ent.IsDeleted == true).ToList();
            }
            else
            {
                data = ProductBLL.GetAll().ToList();
            }
            Store1.DataSource = ExtNet.GirdData.Filter<Models.Product>(data, fc, e);
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductDelete");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Product Product = bitEnt.Products.SingleOrDefault(ent => ent.ProductID == ID);
                    if (Product != null)
                    {
                        Product.IsDeleted = true;
                    }
                }
                int record = bitEnt.SaveChanges();
                ShowNotification("Xóa " + record + " sản phẩm thành công!");
                sm.ClearSelections();
                LoadGridData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnUpdateSelected_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Product product = bitEnt.Products.SingleOrDefault(ent => ent.ProductID == ID);
                    if (product != null)
                    {
                        if (ckUpdateDiscount.Checked) product.IsDiscount = ckIsDiscount.Checked;
                        if (ckUpdateEnabled.Checked) product.IsEnabled = ckIsEnabled.Checked;
                        if (ckUpdateHot.Checked) product.IsHot = ckIsHot.Checked;
                        if (ckUpdateNew.Checked) product.IsNew = ckIsNew.Checked;
                        if (ckUpdateAvaiable.Checked) product.Avaiabled = ckAvaiable.Checked;
                        if (ckUpdateAllowOrder.Checked) product.AllowOrder = ckAllowOrder.Checked;

                        if (ckUpdateManufactor.Checked) product.Manufactor = txtManufactor.Text;
                        if (ckUpdateWarranty.Checked) product.Warranty = txtWarranty.Text;
                        if (ckUpdateSortorder.Checked) product.SortOrder = txtSortOrder.Text.ToInt64TryParse();
                        if (ckUpdateDatetimeDisplay.Checked) product.DatetimeDisplay = datetimeDisplay.SelectedDate;

                        if (ckUpdatePrice.Checked) product.Price = Convert.ToDecimal(txtPrice.Text);
                        if (ckUpdateOldPrice.Checked) product.OldPrice = Convert.ToDecimal(txtOldPrice.Text);
                        if (ckUpdateCost.Checked) product.ProductCost = Convert.ToDecimal(txtCost.Text);
                        if (ckUpdateCallToPrice.Checked) product.CallToPrice = ckCallToPrice.Checked;
                    }
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Cập nhật " + record + " sản phẩm thành công!");
                    Window2.Hide();
                    sm.ClearSelections();
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrash_Click(object sender, DirectEventArgs e)
        {
            if (Session["ProductGridFlag"] != null && Session["ProductGridFlag"].Equals(false))
            {
                Session["ProductGridFlag"] = true;
            }
            else
            {
                Session["ProductGridFlag"] = false;
            }
            //LoadGridData();
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");

        }

        protected void btnTrashEmpty_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductDelete");
                int record = 0;
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                foreach (BitPortal.Models.Product a in bitEnt.Products.Where(ent => ent.IsDeleted == true).ToList())
                {
                    if (ProductBLL.DeletePermanently(a.ProductID) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " sản phẩm trong thùng rác thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrashDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductDelete");
                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (ProductBLL.DeletePermanently(Convert.ToInt64(row.RecordID)) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " sản phẩm trong thùng rác thành công!");
                    sm.ClearSelections();
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnUnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductEdit");

                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Product Product = bitEnt.Products.SingleOrDefault(ent => ent.ProductID == ID);
                    if (Product != null)
                        Product.IsDeleted = false;
                }
                int record = bitEnt.SaveChanges();
                ShowNotification("Bỏ Xóa " + record + " sản phẩm thành công!");
                sm.ClearSelections();
                LoadGridData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        public void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
        }

        protected void btnEditMedia_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                ProductMedia1.BindData(sm.SelectedRows[0].RecordID.ToInt64TryParse());
            }
        }

    }
}